
// 表格分页器总数
export function mTotal(total:number = 0) {
  return `共 ${total} 条`
}

// 表格序号
export function mIndex(pagination: Pagination, index: number): number {
  const { current, pageSize } = pagination
  return (current - 1) * pageSize + (index + 1)
}

// 首字母大写
export function mInitialUpper(str: string) {
  return str.slice(0, 1).toUpperCase() + str.slice(1)
}

// 配置数据格式转换（数组转对象）
type MTransitionItem = {
  [propName: string]: any;
}
export function mTransition(arr:Array<object> = [], key:string = 'value') {
  const obj:MTransitionItem = {}
  arr.forEach((item:MTransitionItem, index) => {
    obj[`${item[key]}`] = {
      '$index_': index,
      ...item
    }
  })
  return obj
}

// 屏蔽手机号
export function mHideTel(tel: number | string) {
  tel = tel + ''
  let reg = /^(\d{3})\d{4}(\d{4})$/;
  return tel.replace(reg, "$1****$2");
}

/**
 * 将数值四舍五入(保留2位小数)后格式化成金额形式
 * 千分位格式展示
 * @param num 数值(Number或者String)
 * @return 金额格式的字符串,如'1,234,567.45'
 * @type String
 */
export function mCurrency(num:number | string = 0):string {
  if (isNaN(Number(num)) || !num) {
    num = "0";
  }
  num = num.toString().replace(/\$|,/g, '');
  num = parseFloat(num)
  let sign = (num == (num = Math.abs(num)));
  num = Math.floor(num * 100 + 0.50000000001);
  let cents:number | string = num % 100;
  num = Math.floor(num / 100).toString();
  if (cents < 10)
    cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
    num = num.substring(0, num.length - (4 * i + 3)) + ',' +
      num.substring(num.length - (4 * i + 3));
  return (((sign) ? '' : '-') + num + '.' + cents);
}

/**
 * 筛选数组项值
 * 
 * 返回值：数据项的指定字段值/数据项/参数value
 */
export function mFilterTerm(value:string, list:Array<any>, valueFieldName:string = 'id', resFieldName:string = 'name') {
  let res = value
  if (Array.isArray(list) && list.length) {
    list.forEach(item => {
      if (item[valueFieldName] == value) {
        res = resFieldName ? item[resFieldName] : item
      }
    })
  }
  return res
}

/**
 * 搜索表单应用
 * @param {*} ref // 目标
 * @param {*} reset  // 是否重置 
 * @param {*} pd // 目标padding
 * @param {*} seat // 间隔
 * @returns 
 */
type SearchForm = {
  ref: any;
  reset: boolean;
  pd: number;
  seat: number
}
export function mSearchForm({ ref, reset = false, pd = 48, seat = 16 }:SearchForm) {
  let showMore = false
  let showText = ''
  
  if (ref) {
    const ele = ref.$el
    const children:HTMLDivElement[] = Array.from(ele.children)
    const searchBox = children[children.length - 1]
    const moreBtn:any = searchBox.getElementsByClassName('ant-btn-link')[0]
    const icons:any = searchBox.getElementsByClassName('ant-btn-icon-only')
    const Arr = [searchBox, ...children.splice(0, children.length - 1)]

    // 判断当前是否展开
    let isShow = true
    Arr.forEach(item => {
      if (item.style.display == 'none') {
        isShow = false
      }
    })
    // 获取隐藏的dom数组
    let total = 0
    const hidden = []
    if (isShow || reset) {
      Arr.forEach((item, index) => {
        if (reset) {
          item.style.display = 'inline-block'
        }
        total = total + (item.offsetWidth + seat)
        if (total >= ele.offsetWidth - pd && index > 1) {
          item.style.display = 'none'
          hidden.push(item)
        }
      })

    }
    if (hidden.length) {
      showMore = true
      showText = '展开'
      icons[0].style.display = 'inline-block'
      icons[1].style.display = 'none'
      if (reset) {
        moreBtn.style.display = 'inline-block'
      }
    } else {
      Arr.forEach(item => {
        item.style.display = 'inline-block'
      })
      if (moreBtn.innerHTML) {
        showText = '收起'
        icons[0].style.display = 'none'
        icons[1].style.display = 'inline-block'
      }
      if (reset) {
        moreBtn.style.display = 'none'
      }
    }

    if (moreBtn && showText) {
      showMore = true
      moreBtn.innerHTML = `<span>${showText}</span>`

      if (reset) {
        if (hidden.length) {
          searchBox.style.float = 'right'
        } else {
          icons[0].style.display = 'none'
          icons[1].style.display = 'none'
          searchBox.style.float = ''
        }
      } else {
        searchBox.style.float = 'right'
      }
    } else {
      moreBtn.style.display = 'none'
      icons[0].style.display = 'none'
      icons[1].style.display = 'none'
      if (reset) {
        searchBox.style.float = ''
      }
    }
  }
  return showMore
}

/**
 * 验证是否json
 * @param {*} str  //传入数据
 * @returns 
 */
export function mIsJson(str:string) {
  if (typeof str == 'string') {
    try {
      let obj = JSON.parse(str);
      if (typeof obj == 'object' && obj) {
        return true;
      } else {
        return false;
      }

    } catch (e) {
      return false;
    }
  }
}

export default {
  mTotal,
  mIndex,
  mInitialUpper,
  mTransition,
  mHideTel,
  mCurrency,
  mFilterTerm,
  mSearchForm,
  mIsJson
}